package com.cykj.enterprise.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.cykj.common.dto.ResponseDTO;
import com.cykj.common.entity.HrDO;
import com.cykj.enterprise.service.IHrService;
import com.cykj.enterprise.vo.CompanyInfoVO;
import com.cykj.enterprise.vo.HrVO;
import com.github.pagehelper.PageHelper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @BelongsProject: smart_recruit_backend
 * @BelongsPackage: com.cykj.enterprise.controller
 * @Author: 蔡张君逸
 * @CreateTime: 2025-08-19 15:52
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@CrossOrigin
@RequestMapping("hr")
public class HrController {
    @Autowired
    private IHrService hrService;
//    登录
    @Operation(summary = "登录")
    @PostMapping("login")
    public ResponseDTO login(@RequestBody HrVO hrVO, HttpSession httpSession) {
        return hrService.hrLogin(hrVO.getAccount(),hrVO.getPassword(),hrVO.getCode(),httpSession);
    }
//    验证码
    @Operation(summary = "验证码")
    @GetMapping("validateCodeImage")
    public ResponseDTO validateCodeImage(HttpSession httpSession) {
        return hrService.getValidateCodeImage(httpSession);
    }
//    邮箱 发送验证码
    @Operation(summary = "邮箱验证码")
    @GetMapping("emailValidateCode")
    public ResponseDTO emailValidateCode(@RequestParam("address") String address,HttpSession httpSession) {
    return hrService.emailValidateCode(address,httpSession);
}
//    注册
    @Operation(summary = "注册")
    @PostMapping("register")
    public ResponseDTO register(@RequestBody HrVO hrVO, HttpSession httpSession) {
        return hrService.register(hrVO,httpSession);
    }

//    退出登录
@Operation(summary = "退出登录")
@GetMapping("logOut")
public ResponseDTO logOut() {
        StpUtil.logout();
    return ResponseDTO.success();
}

//    验证登录
    @Operation(summary = "校验登录状态")
    @SaCheckLogin
    @GetMapping("checkLogin")
    public ResponseDTO checkLogin() {
        try {
            return ResponseDTO.success(StpUtil.getLoginIdAsInt());
        } catch (Exception e) {
            return ResponseDTO.error("未登录");
        }
    }
    //    获取登录者信息
    @Operation(summary = "获取登录者信息")
    @SaCheckLogin
    @GetMapping("getLoginInfo")
    public ResponseDTO getLoginInfo() {
        int hrId = StpUtil.getLoginIdAsInt();
        return hrService.getLoginInfo(hrId);
    }
//更新任意hr信息
    @Operation(summary = "更新任意hr信息")
    @SaCheckLogin
    @PostMapping("updateInfo")
    public ResponseDTO updateInfo(@RequestBody  HrDO hrDO) {
        return hrService.updateInfo(hrDO);
    }
//获取子账号
    @Operation(summary = "获取子账号")
    @Parameter(name = "companyId",description = "企业id",in = ParameterIn.QUERY)
    @Parameter(name = "pageNum",description = "页码",in = ParameterIn.QUERY)
    @Parameter(name = "pageSize",description = "页面大小",in = ParameterIn.QUERY)
    @SaCheckLogin
    @GetMapping("getSubHrs")
    public ResponseDTO getSubHrs(@RequestParam("companyId") int companyId,@RequestParam("pageNum") int pageNum, @RequestParam("pageSize")int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return hrService.getSubHrs(companyId);
    }
//    启用禁用子账号
    @Operation(summary = "启用禁用子账号")
    @Parameter(name = "hrId",description = "hrId",in = ParameterIn.QUERY)
    @SaCheckLogin
    @GetMapping("changeStatus")
    public ResponseDTO changeStatus(@RequestParam("hrId")int hrId) {
        return hrService.changeStatus(hrId);
    }
    //创建子hr
    @Operation(summary = "创建子hr")
    @SaCheckLogin
    @GetMapping("addSubHr")
    public ResponseDTO addSubHr(HrDO hrDO) {
        return hrService.addSubHr(hrDO);
    }

    //    更改头像
    @Operation(summary = "更改头像")
    @Parameter(name = "file",description = "图片文件对象",in = ParameterIn.QUERY)
    @SaCheckLogin
    @PostMapping("reviseUserImg")
    public ResponseDTO reviseUserImg(@RequestParam("file") MultipartFile file) {
        return hrService.reviseUserImg(file);
    }

    //获取用户基础信息加上收藏未收藏是否拉黑
    @Operation(summary = "获取用户基础信息加上收藏未收藏是否拉黑")
    @Parameter(name = "userId",description = "用户Id",in = ParameterIn.QUERY)
    @SaCheckLogin
    @GetMapping("getUserInfo")
    public ResponseDTO getUserInfo(@RequestParam("userId") int userId) {
        return ResponseDTO.success(hrService.getUserInfo(userId));
    }
    //获取用户简历
    @Operation(summary = "获取用户简历")
    @Parameter(name = "userId",description = "用户Id",in = ParameterIn.QUERY)
    @SaCheckLogin
    @GetMapping("getUserOnlineResume")
    public ResponseDTO getUserOnlineResume(@RequestParam("userId") int userId) {
        return hrService.getUserOnlineResume(userId);
    }


    /**
    * @Description: 确认hr是否已经通过企业资质认证，或已经上传认证资料
    * @Param: []
    * @return: com.cykj.common.dto.ResponseDTO
    */
    @Operation(description = "确认hr是否已经通过企业资质认证，或已经上传认证资料")
    @GetMapping("/hrCompanyCheck")
    public ResponseDTO hrCompanyCheck(){
        return hrService.hrCompanyCheck();
    }


    /**
    * @Description: 企业资质审核提交
    * @Param: [companyName, enterpriseCode, license, prove, companyIcon]
    * @return: com.cykj.common.dto.ResponseDTO
    */
    @Operation(summary = "企业资质审核提交")
    @PostMapping("/submitCompanyData")
    public ResponseDTO submitCompanyData(@RequestParam("companyName")String companyName,
                                         @RequestParam("enterpriseCode")String enterpriseCode,
                                         @RequestParam("license")MultipartFile license,
                                         @RequestParam("prove")MultipartFile prove,
                                         @RequestParam("companyIcon")MultipartFile companyIcon){
        CompanyInfoVO companyInfoVO = new CompanyInfoVO();
        companyInfoVO.setCompanyName(companyName);
        companyInfoVO.setEnterpriseCode(enterpriseCode);
        companyInfoVO.setLicense(license);
        companyInfoVO.setProve(prove);
        companyInfoVO.setCompanyIcon(companyIcon);
        return hrService.submitCompanyData(companyInfoVO);
    }
}